package com.gxuwz.medical.domain.chronicdis;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;

import com.gxuwz.medical.database.DBUtil;

/**
 * 慢性病分类管理类
 * @author 沉羽墨玖
 *
 */
public class Chronicdis {
	private String illCode;   //疾病编码
	private String illName;  //疾病名称
	private String pyCode;   //拼音码
	private String wbCode;   //五笔码
	
	public Chronicdis(){
		
	}
	
	public Chronicdis(String illCode,String illName,String pyCode,String wbCode){
		super();
		this.illCode =illCode;
		this.illName =illName;
		this.pyCode =pyCode;
		this.wbCode =wbCode;
		
	}
	public void load(String illCode)throws SQLException{
		Connection conn=null;
		 PreparedStatement pstmt=null;
		 ResultSet rs=null;
		try{
			conn = DBUtil.getConn();
			pstmt = conn.prepareStatement("select * from chronicdis where illCode=?");
			int index = 1;
			pstmt.setString(index, illCode);
			rs=pstmt.executeQuery();
		 if(rs != null && rs.next()){
			 this.illCode=rs.getString("illCode");
			 this.illName =rs.getString("illName");
			 this.pyCode =rs.getString("pyCode");
			 this.wbCode =rs.getString("wbCode");
		 }
		}catch(SQLException e){
			throw new SQLException("Chronicdis with id " + illCode
					+ " could not be loaded from the database.");
		}finally {
			DBUtil.close(rs, pstmt, conn);
		}
		
	}
	/**
	 * 添加慢性病信息
	 * @throws SQLException
	 */
	public void add()throws SQLException{
		Connection conn=null;
       try{
    	    conn=DBUtil.getConn();
    	    conn.setAutoCommit(false);
    	   //2；调用saveToDB方法将新添加的数据保存到数据库
    	    saveToDB(conn);
    	    conn.commit();
       }catch(SQLException e){
    	   throw e;
       }finally{
    	   DBUtil.close(conn);
       }
		
	}
	/**
	 * 将新添加的数据保存到数据库
	 * @param conn
	 * @throws SQLException
	 */
	private void saveToDB(Connection conn)throws SQLException{
		PreparedStatement pstmt=null;
		try{
			StringBuffer sqlBuff=new StringBuffer();
			sqlBuff.append("insert into chronicdis");
			sqlBuff.append("(illCode,illName,pyCode,wbCode)");
			sqlBuff.append("values(?,?,?,?)");
			pstmt=conn.prepareStatement(sqlBuff.toString());
			 //设置动态参数对应的值
			 pstmt.setString(1, this.illCode);
			 pstmt.setString(2, this.illName);
			 pstmt.setString(3, this.pyCode);
			 pstmt.setString(4, this.wbCode);
			 pstmt.executeUpdate(); 
			
		}catch(SQLException e){
			throw e;
		}finally{
			DBUtil.close(pstmt);
			
		}
		
		
	}
	/**
	 * 根据疾病编号删除慢性病信息
	 * @param illCode
	 * @throws SQLException
	 */
	public void del(String illCode)throws SQLException{
		Connection conn=null;
		this.illCode=illCode;
       try{
    	    conn=DBUtil.getConn();
			conn.setAutoCommit(false);
    	   //调用delFromDB方法
    	    deleteFromDB(conn);
    	  //提交事务
			conn.commit();
       }catch(SQLException e){
    	   conn.rollback();
    	   throw e;
       }finally{
    	   DBUtil.close(conn);
       }
		
	}
	/**
	 * 删除慢性病信息
	 * @param conn
	 * @throws SQLException
	 */
	private void deleteFromDB(Connection conn)throws SQLException{
		PreparedStatement pstmt=null;
		try{
			StringBuffer sqlBuff=new StringBuffer("delete from chronicdis where illCode=?");
			pstmt=conn.prepareStatement(sqlBuff.toString());
			pstmt.setString(1, this.illCode);
			pstmt.executeUpdate(); 
			
		}catch(SQLException e){
			throw new SQLException("Failed to delete record from table !", e);
		}finally{
			DBUtil.close(pstmt);
			
		}
		
		
	}
	
	/**
	 * 修改慢性病信息
	 * @throws SQLException
	 */
	public void edit()throws SQLException{
		Connection conn=null;
		try{
			 conn =DBUtil.getConn();
			 conn.setAutoCommit(false);
			//更新慢性病信息到数据库
			 editToDB(conn);
			 conn.commit();
		}catch(SQLException e){
			conn.rollback();
			throw e;
		}finally{
			DBUtil.close(conn);
		}
	}
	/**
	 * 更新慢性病信息到数据库
	 * @param conn
	 * @throws SQLException
	 */
	private void editToDB(Connection conn)throws SQLException{
		PreparedStatement pstmt=null;
		try{
			  StringBuffer sqlBuff=new StringBuffer("update chronicdis t set t.illName=?,t.pyCode=?,t.wbCode=? where t.illCode=?");
			  pstmt=conn.prepareStatement(sqlBuff.toString());
			 //设置动态参数对应的值
			 pstmt.setString(1, this.illName);
			 pstmt.setString(2, this.pyCode);
			 pstmt.setString(3, this.wbCode);
			 pstmt.setString(4, this.illCode);
			 pstmt.executeUpdate(); 
			
		}catch(SQLException e){
			throw new SQLException("Failed to update chronicdis  !", e);
		}finally{
			DBUtil.close(pstmt);
		}
		
	}
	
	public Chronicdis(String illCode)throws Exception{
		load(illCode);
		
	}
	public String getIllCode() {
		return illCode;
	}

	public void setIllCode(String illCode) {
		this.illCode = illCode;
	}

	public String getIllName() {
		return illName;
	}

	public void setIllName(String illName) {
		this.illName = illName;
	}

	public String getPyCode() {
		return pyCode;
	}

	public void setPyCode(String pyCode) {
		this.pyCode = pyCode;
	}

	public String getWbCode() {
		return wbCode;
	}

	public void setWbCode(String wbCode) {
		this.wbCode = wbCode;
	}

}


